package org.dandelion.papercheck.dto;

import lombok.Data;

@SuppressWarnings({"rawtypes", "unchecked"})
@Data
public class BaseResult<T> {
    private Integer code;
    private String msg;
    private T data;

    public static BaseResult success(Object obj) {
        var ret = new BaseResult();
        ret.setCode(BaseResultEnum.SUCCESS.getCode());
        ret.setMsg(BaseResultEnum.SUCCESS.getMsg());
        ret.setData(obj);
        return ret;
    }

    public static BaseResult logout() {
        var ret = new BaseResult();
        ret.setCode(BaseResultEnum.UN_LOGIN.getCode());
        ret.setMsg(BaseResultEnum.UN_LOGIN.getMsg());
        ret.setData("");
        return ret;
    }

    public static BaseResult JwtError() {
        var ret = new BaseResult();
        ret.setCode(BaseResultEnum.JWT_ERROR.getCode());
        ret.setMsg(BaseResultEnum.JWT_ERROR.getMsg());
        ret.setData("");
        return ret;
    }

    public static BaseResult illegalAuthority() {
        var ret = new BaseResult();
        ret.setCode(BaseResultEnum.IllEGAl_AUTH.getCode());
        ret.setMsg(BaseResultEnum.IllEGAl_AUTH.getMsg());
        ret.setData("");
        return ret;
    }

    public static BaseResult illegalAuthority(Object data) {
        var ret = new BaseResult();
        ret.setCode(BaseResultEnum.IllEGAl_AUTH.getCode());
        ret.setMsg(BaseResultEnum.IllEGAl_AUTH.getMsg());
        ret.setData(data);
        return ret;
    }

    public static BaseResult success() {
        var zero = new String[0];
        return success(zero);
    }

    public static BaseResult error(Integer code, String msg) {
        var BaseResult = new BaseResult();
        var zero = new String[0];
        BaseResult.setCode(code);
        BaseResult.setMsg(msg);
        BaseResult.setData(zero);
        return BaseResult;
    }

    public static BaseResult error(Integer code, String msg, Object data) {
        var BaseResult = new BaseResult();
        var zero = new String[0];
        BaseResult.setCode(code);
        BaseResult.setMsg(msg);
        BaseResult.setData(data);
        return BaseResult;
    }

    enum BaseResultEnum {
        SUCCESS(200, "success"),
        BAD_REQUEST(400, "bad request"),
        FORBID_REQUEST(403, "forbidden"),
        NOT_FOUND(404, "not found"),
        UNKNOWN_ERROR(500, "unknown error"),
        // JWT错误
        JWT_ERROR(406, "Illegal Identity"),
        // 权限不足
        IllEGAl_AUTH(407, "Illegal Auth"),
        //未登录
        UN_LOGIN(405, "not login"),
        // 邮箱格式错误
        EMAIL_FORMAT_ERR(601, "email format error"),
        // 手机格式错误
        PHONE_FORMAT_ERR(603, "phone format error");

        private Integer code;
        private String msg;

        BaseResultEnum(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
}
